<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/5/29
 * Time: 14:40
 */

namespace app\index\controller;

use app\common\controller\IndexController;
use app\admin\model\Order as OrderModel;
use app\Request;
use app\admin\model\UserInfo;
use app\admin\model\MallGoods;
use app\admin\model\Calculation;
use app\admin\model\UserAmountLog;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\db\exception\DbException;
use app\admin\model\UserLevel;
use app\admin\model\User;

/**
 * Class Order
 * @package app\index\controller
 * User : Orange Tel: 181-3414-1353 QQ:1139552211
 * Date    : 2021/6/9 下午3:27
 */
class Order extends IndexController {

    /**
     * 下单
     * @param Request $request
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * User : Orange Tel: 181-3414-1353 QQ:1139552211
     * Date    : 2021/6/9 下午3:27
     */
    public function add_order(Request $request) {
        $user_id = $request->uid;

        $all      = input();
        $validate = \think\facade\Validate::rule([
            'type_id' => 'require|number',
            'type'    => 'require|number',
            'number'  => 'require|number',
        ]);
        if (!$validate->check($all)) {
            $this->error(0, $validate->getError());
        }
        //获取并判断资产是否充足
        $user_info = UserInfo::instance()->getCoinByUid($user_id);
        if (empty($user_info)) {
            $this->error('USDT资产不足');
        }
        //下单各类型判断
        $order_data = [
            'goods_type'   => 1,
            'money'        => 0,
            'entrust_days' => 0,
            'status'       => 10,
            'produce'      => 0,
            'information'  => '',
            'produce_type' => 0,
            'days_time'    => 1
        ];
        switch ($all['type']) {
            case OrderModel::ORDER_TG:
            case OrderModel::ORDER_KJ:
            {
                $stock       = MallGoods::instance()->isStockNumber($all['number']);
                $goods_model = MallGoods::instance();
                $order_data  = OrderModel::instance()->getOrderNodeInfo($all, $order_data);
                break;
            }
            case OrderModel::ORDER_SL:
            {
                $stock       = Calculation::instance()->isStockNumber($all['number']);
                $goods_model = Calculation::instance();
                $order_data  = OrderModel::instance()->getOrderPowerInfo($all, $order_data);
                break;

            }
        }
        //判断库存是否充足
        if (empty($stock)) {
            $this->error('产品库存不足');
        }
        //判断资产是否充足
        if ($order_data['money'] > $user_info['assets']) {
            $this->error('资产不足');
        }

        $data = [
            'order_number' => get_order(15, 'YH'),
            'uid'          => $user_id,
            'type'         => $all['type'],
            'type_id'      => $all['type_id'],
            'information'  => $order_data['information'],
            'goods_type'   => $order_data['goods_type'],
            'money'        => $order_data['money'],
            'number'       => $all['number'],
            'entrust_days' => $order_data['entrust_days'],
            'status'       => $order_data['status'],
            'produce'      => $order_data['produce'],
            'uid_phone'    => isset($all['uid_phone']) ? $all['uid_phone'] : '',
            'address'      => isset($all['address']) ? $all['address'] : '',
            'produce_type' => $order_data['produce_type'],
            'days_time'    => get_someday_time($order_data['days_time']),
            'create_time'  => time()

        ];
        //扣钱
        UserInfo::instance()->decUserMoney($user_id, $order_data['money']);
        //加流水
        UserAmountLog::instance()->addLog($user_id, $order_data['money'], 7,6);
        //扣库存
        $goods_model->decProductStock($all);
        //入单
        $order_id = OrderModel::instance()->insertGetId($data);
        //等级提升
        $info = User::instance()->where(['id' => $user_id])->find()->toArray();
        UserLevel::instance()->userLevel($info, true);

        if (empty($order_id)) {
            $this->error('下单失败');
        } else {
            $this->success('下单成功', ['order_id' => $order_id]);
        }
    }
}